import Vue from 'vue'
import VueRouter from 'vue-router'
import utils from "@/plugins/utils.js"
import http from "@/plugins/http.js"
import routerList from "@/router/generateRouter"
Vue.use(VueRouter)
let isToken = (()=>{
  return process.env.VUE_APP_IS_TOKEN =='isToken' ? true : false
})()
console.log(routerList)
const routes = [
  ...routerList
]

const router = new VueRouter({
  routes:routes,
  scrollBehavior:()=>{return { x: 0, y: 0 }} //跳转路有之后回到原始位置
})



// 加载完成之前做点什么  用于做动态加载路由
http://jsonplaceholder.typicode.com/posts
// router.onReady(async()=>{
//  let routerData = await (async function() {
//    let result = {}
//     await http({
//       method:"get",
//       url:"/posts"
//     }).then(res=>{
//       result = res
//     })
//     return result

//   })()
//   console.log(routerData)
// })
// to 你去哪里
// from 你从哪里来
// next 你下一步要做什么

router.beforeEach((to,from,next)=>{
  
  if(!isToken){
    return next()
  }
  // 判断是否有token  
  let hasToken = utils.getToken("token")   //传token设置的key值
  if(hasToken){
    if(to.path == '/login'){
      next({
        path:"/"
      });
    }else{
      next()
    }
  }else{
    if(to.path == '/login'){
      next()
    }else{
      next({path:'/login'});
    }
  }
})
export default router
